package com.ruoyi.lhb.util;

import java.text.SimpleDateFormat;
import java.util.*;

public class Util {
    public static String getTime(){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(System.currentTimeMillis());
        return format.format(date);
    }
    public static String getDate(){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date(System.currentTimeMillis());
        return format.format(date);
    }

    // 获取当前一周的一星期的日期
    public static List<String> GetCurrentWeekAllDate(){
        // 获取当天的毫秒时间
        long currentTimeMillis = System.currentTimeMillis();
        List<String> list = getAllWeekDayDateByMillis(currentTimeMillis);
        return list;
    }
    // 获取指定日期毫秒时间的当周一星期的日期
    private static List<String> getAllWeekDayDateByMillis(long timeMills){
        List<String> list = new ArrayList<String>();
        // 得到指定时间是周几
        String week = getWhatDaySomeday(timeMills);
        // 记录与周一的间隔天数
        int dayFromMonday=getHowManyDayFromMonday(week);
        // 获取这周第一天毫秒值
        long dayMillis = 24*60*60*1000;
        // 获取这周第一天的日子
        long firstOfWeekMillis = timeMills - dayFromMonday * dayMillis;
        // 使用 for 循环得到当前一周的日子（7天的日子）
        for(long i = firstOfWeekMillis;i<firstOfWeekMillis+ 7 *dayMillis;i+=dayMillis) {
            Date targetDate = new Date(i);
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            String targetDay = format.format(targetDate);
            list.add(targetDay);
        }
        return list;
    }
    // 获取指定任意日期当周的一星期的日期
    private static void GetSomedayWeekAllDate(){
        // 指定天的毫秒时间
        long currentTimeMillis = getWhatDaySomeDateMillis("2019-10-15");
        List<String> list = getAllWeekDayDateByMillis(currentTimeMillis);
        for(String date:list) {
            System.out.println("date : "+date);
        }
    }

    // someDataStr 格式："yyyy-MM-dd"
    // 返回 时间毫秒
    private static long getWhatDaySomeDateMillis(String someDataStr) {

        Date date= new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        try {
            date = format.parse(someDataStr);
            return date.getTime();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return -1;
    }

    // 获取指定日期毫秒时间得到 星期几
    private static String getWhatDaySomeday(long timeMillis) {

        Date toDayDate = new Date(timeMillis);
        SimpleDateFormat formatE = new SimpleDateFormat("E");
        String week =null;
        try {
            week = formatE.format(toDayDate);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return week;
    }

    // 根据 星期几到获取与 星期一 相差几天
    private static int getHowManyDayFromMonday(String someDay) {
        int day = 0;
        switch (someDay) {
            case "星期一":
                day = 0;
                break;
            case "星期二":
                day = 1;
                break;
            case "星期三":
                day = 2;
                break;
            case "星期四":
                day = 3;
                break;
            case "星期五":
                day = 4;
                break;
            case "星期六":
                day = 5;
                break;
            case "星期日":
                day = 6;
                break;
            default:
                System.out.println("不存在这样的星期几 : " + someDay);
                break;
        }
        return day;
    }


    //获取本月所有日期
    public static List<String> getMonthFullDay() {
        String date=getDate();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        List<String> fullDayList = new ArrayList<String>();
        int year = Integer.parseInt(date.substring(0, 4));
        int month = Integer.parseInt(date.substring(5, 7));
        int day = 1;// 所有月份从1号开始
        Calendar cal = Calendar.getInstance();// 获得当前日期对象
        cal.clear();// 清除信息
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month - 1);// 1月从0开始
        cal.set(Calendar.DAY_OF_MONTH, day);
        int count = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        for (int j = 0; j <= (count - 1); ) {
            if (sdf.format(cal.getTime()).equals(getLastDay(year, month)))
                break;
            cal.add(Calendar.DAY_OF_MONTH, j == 0 ? +0 : +1);
            j++;
            fullDayList.add(sdf.format(cal.getTime()));
        }
        return fullDayList;
    }

    public static String getLastDay(int year, int month) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month);
        cal.set(Calendar.DAY_OF_MONTH, 0);
        return sdf.format(cal.getTime());
    }

    //拿到近半年数据
    public static List<HashMap<String, Object>> getLastSixMonth(){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat monthFormat = new SimpleDateFormat("yyyy-MM");
        ArrayList<HashMap<String, Object>> list = new ArrayList<>();
        HashMap<String, Object> map = new HashMap<>();
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.DAY_OF_MONTH,1);
        map.put("firstDay",format.format(cal.getTime()));
        cal.set(Calendar.DAY_OF_MONTH,cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        map.put("lastDay",format.format(cal.getTime()));
        map.put("monthDate",monthFormat.format(cal.getTime()));
        map.put("month",cal.get(Calendar.MONTH) + 1);
        list.add(map);
        for(int i=0; i<6; i++){
            HashMap<String, Object> resultMap = new HashMap<>();
            cal.set(Calendar.DAY_OF_MONTH,1);
            cal.add(Calendar.MONTH, -1);
            String firstDay = format.format(cal.getTime());
            String monthDate = monthFormat.format(cal.getTime());
            cal.set(Calendar.DAY_OF_MONTH,cal.getActualMaximum(Calendar.DAY_OF_MONTH));
            String lastDay = format.format(cal.getTime());
            resultMap.put("firstDay",firstDay);
            resultMap.put("lastDay",lastDay);
            resultMap.put("monthDate",monthDate);
            resultMap.put("month",cal.get(Calendar.MONTH) + 1);
            list.add(resultMap);
        }
        return list;
    }
}
